package org.dromara.knowledge.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.knowledge.domain.KnowledgePromptTemplate;
import org.dromara.knowledge.domain.bo.KnowledgePromptTemplateBo;
import org.dromara.knowledge.domain.vo.KnowledgePromptTemplateVo;
import org.dromara.knowledge.mapper.KnowledgePromptTemplateMapper;
import org.dromara.knowledge.service.KnowledgePromptTemplateService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 知识库提示模板Service业务层处理
 *
 * @author ruoyi
 */
@RequiredArgsConstructor
@Service
public class KnowledgePromptTemplateServiceImpl extends ServiceImpl<KnowledgePromptTemplateMapper, KnowledgePromptTemplate> implements KnowledgePromptTemplateService {

    private final KnowledgePromptTemplateMapper baseMapper;

    /**
     * 查询知识库提示模板
     */
    @Override
    public KnowledgePromptTemplateVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询知识库提示模板列表
     */
    @Override
    public TableDataInfo<KnowledgePromptTemplateVo> queryPageList(KnowledgePromptTemplateBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<KnowledgePromptTemplate> lqw = buildQueryWrapper(bo);
        Page<KnowledgePromptTemplateVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询知识库提示模板列表
     */
    @Override
    public List<KnowledgePromptTemplateVo> queryList(KnowledgePromptTemplateBo bo) {
        LambdaQueryWrapper<KnowledgePromptTemplate> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<KnowledgePromptTemplate> buildQueryWrapper(KnowledgePromptTemplateBo bo) {
        LambdaQueryWrapper<KnowledgePromptTemplate> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getTemplateName()), KnowledgePromptTemplate::getTemplateName, bo.getTemplateName());
        lqw.like(StringUtils.isNotBlank(bo.getTemplateContent()), KnowledgePromptTemplate::getTemplateContent, bo.getTemplateContent());
        lqw.eq(StringUtils.isNotBlank(bo.getCategory()), KnowledgePromptTemplate::getCategory, bo.getCategory());
        return lqw;
    }

    /**
     * 新增知识库提示模板
     */
    @Override
    public Boolean insertByBo(KnowledgePromptTemplateBo bo) {
        KnowledgePromptTemplate add = BeanUtil.toBean(bo, KnowledgePromptTemplate.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改知识库提示模板
     */
    @Override
    public Boolean updateByBo(KnowledgePromptTemplateBo bo) {
        KnowledgePromptTemplate update = BeanUtil.toBean(bo, KnowledgePromptTemplate.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(KnowledgePromptTemplate entity) {
        // TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除知识库提示模板
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 根据分类查询模板列表
     */
    @Override
    public List<KnowledgePromptTemplateVo> queryListByCategory(String category) {
        LambdaQueryWrapper<KnowledgePromptTemplate> lqw = Wrappers.lambdaQuery();
        lqw.eq(KnowledgePromptTemplate::getCategory, category);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 根据模板名称查询模板信息
     */
    @Override
    public KnowledgePromptTemplateVo queryByTemplateName(String templateName) {
        LambdaQueryWrapper<KnowledgePromptTemplate> lqw = Wrappers.lambdaQuery();
        lqw.eq(KnowledgePromptTemplate::getTemplateName, templateName);
        return baseMapper.selectVoOne(lqw);
    }
}